package licode.unisop.log.service.infrastructure.repository.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import licode.unisop.log.client.api.LastLoginLogApi;
import licode.unisop.log.client.dto.LoginLogDTO;
import licode.unisop.log.client.req.LastLoginLogIn;
import licode.unisop.log.client.req.LoginLogIn;
import licode.unisop.log.service.infrastructure.assembler.LogAssembler;
import licode.unisop.log.service.infrastructure.repository.mp.mapper.LastLoginLogMapper;
import licode.unisop.log.service.infrastructure.repository.po.LastLoginLogPO;
import licode.unisop.types.util.IdGenUtil;
import licode.unisop.unilite.dp.PageData;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实现用户最后一次登录接口
 *
 * @author WR
 * @since 2016/7/12 8:59
 */
@Primary
@Component
public class LastLoginLogApiImpl
        extends ServiceBase<LastLoginLogMapper, LastLoginLogPO>
        implements LastLoginLogApi {
    @Resource
    private LogAssembler logAssembler;

    @Override
    public void save(LoginLogDTO source) {
        LastLoginLogPO oldItem = super.lambdaQuery()
                .eq(LastLoginLogPO::getUserId, source.getUserId())
                .eq(LastLoginLogPO::getPlatformId, source.getPlatformId())
                .eq(LastLoginLogPO::getClientId, source.getClientId())
                .eq(LastLoginLogPO::getTerminalId, source.getTerminalId())
                .one();
        LastLoginLogPO loginLogPo = logAssembler.toLastLoginLogDto(source);
        if (null != oldItem) {
            loginLogPo.setId(oldItem.getId());
            super.updateById(loginLogPo);
        } else {
            loginLogPo.setId(IdGenUtil.buildLoginLogId());
            super.save(loginLogPo);
        }
    }

    @Override
    public List<LoginLogDTO> find(LastLoginLogIn req) {
        return logAssembler.fromLastLoginLog(super.lambdaQuery()
                .eq(LastLoginLogPO::getUserId, req.getUserId())
                .eq(StrUtil.isNotBlank(req.getPlatformId()),
                        LastLoginLogPO::getPlatformId, req.getPlatformId())
                .eq(StrUtil.isNotBlank(req.getClientId()),
                        LastLoginLogPO::getClientId, req.getClientId())
                .eq(StrUtil.isNotBlank(req.getTerminalId()),
                        LastLoginLogPO::getTerminalId, req.getTerminalId())
                .orderByDesc(LastLoginLogPO::getLoginTime)
                .list());
    }

    @Override
    public PageData<LoginLogDTO> query(String platformId, LoginLogIn req) {
        Page<LastLoginLogPO> page = new Page<>(req.getCurrent(),
                req.getPageSize());
        page = super.lambdaQuery()
                .eq(StrUtil.isNotBlank(platformId),
                        LastLoginLogPO::getPlatformId, platformId)
                .like(StrUtil.isNotBlank(req.getUserName()),
                        LastLoginLogPO::getUserName, req.getUserName())
                .like(StrUtil.isNotBlank(req.getPlatformName()),
                        LastLoginLogPO::getPlatformName, req.getPlatformName())
                .le(null != req.getEndDate(),
                        LastLoginLogPO::getLoginTime, req.getEndDate())
                .ge(null != req.getStartDate(),
                        LastLoginLogPO::getLoginTime, req.getStartDate())
                .page(page);

        return convertPage(page, (List<LastLoginLogPO> data) ->
                logAssembler.fromLastLoginLog(data));
    }
}
